/*
 * 
 * 
 * 
 */
package com.hboxs.asl.controller.admin;

import com.hboxs.asl.Message;
import com.hboxs.asl.Pageable;
import com.hboxs.asl.entity.Category;
import com.hboxs.asl.entity.Course;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.ProductImage;
import com.hboxs.asl.qcloudLVB.entity.Live;
import com.hboxs.asl.qcloudLVB.service.LiveService;
import com.hboxs.asl.service.CategoryService;
import com.hboxs.asl.service.CourseService;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.util.DateUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.servlet.http.HttpSession;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Controller - 课程
 */
@Controller("adminCourseController")
@RequestMapping("/asl-admin/course")
public class CourseController extends BaseController {

    @Resource(name = "courseServiceImpl")
    private CourseService courseService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "categoryServiceImpl")
    private CategoryService categoryService;
    @Resource(name = "liveServiceImpl")
    private LiveService liveService;

    /**
     * 添加
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(ModelMap model, HttpSession session) {
        List<Member> memberList = memberService.findAll();
        List<Category> categoryList = categoryService.findAll();
        model.addAttribute("members", memberList);
        model.addAttribute("categories", categoryList);
        session.setAttribute(IMAGE_TYPE, ProductImage.imageType.course);
        return "/admin/course/add";
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(Long memberId,Long categoryId, Course course, String pattern, RedirectAttributes redirectAttributes) {
        course.setDatePattern(DateUtils.string2Date(pattern));
        course.setPubdate(course.getDatePattern().getTime());
        Member member = memberService.find(memberId);
        if (member != null) {
            course.setMember(member);
        }
        Category category = categoryService.find(categoryId);
        if (category != null) {
            course.setCategory(category);
        }
        if (course.getHotSearch() == null) {
            course.setHotSearch(false);
        }
        if (course.getEncrypt() == null) {
            course.setEncrypt(false);
        }
        if (course.getCharge() == null) {
            course.setCharge(false);
            course.setPrice(BigDecimal.ZERO);
        }
        course.setHotSearch(false);
        course.setType(Course.Type.trailer);
        course.setCourseType(Course.CourseType.nolive);
        courseService.save(course);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 编辑
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String edit(Long id, ModelMap model, HttpSession session) {
        session.setAttribute(IMAGE_TYPE, ProductImage.imageType.course);
        model.addAttribute("course", courseService.find(id));
        return "/admin/course/edit";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Course course,String pattern, RedirectAttributes redirectAttributes) {
        if (course.getEncrypt() != null && course.getEncrypt()) {
            if (course.getCharge() == null || !course.getCharge() ) {
                addFlashMessage(redirectAttributes, Message.error("加密状态需要设置收费"));
                return "redirect:edit.htm?id="+course.getId();
            }
        }
        course.setDatePattern(DateUtils.string2Date(pattern));
        course.setPubdate(course.getDatePattern().getTime()/1000);
        if (course.getEncrypt() == null) {
            course.setEncrypt(false);
        }
        if (course.getCharge() == null) {
            course.setCharge(false);
            course.setPrice(BigDecimal.ZERO);
        }
        if (course.getHotSearch() == null) {
            course.setHotSearch(false);
        }
        if (!course.getEncrypt()) {
            course.setPassword(null);
        }
        courseService.update(course, "type", "courseType", "member", "category", "live");
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:list.htm";
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Pageable pageable, ModelMap model) {
        model.addAttribute("page", courseService.findPage(pageable));
        return "/admin/course/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public
    @ResponseBody
    Message delete(Long[] ids) {
        for (Long id : ids) {
            Course course = courseService.find(id);
            Live live = course.getLive();
            if (live != null) {
                liveService.delete(live.getId());
            } else {
                courseService.delete(id);
            }
        }
        return SUCCESS_MESSAGE;
    }

}
